package com.orange.opengl.vbo;

import java.nio.FloatBuffer;

import com.orange.engine.device.Device;
import com.orange.opengl.IGLES20;
import com.orange.opengl.vbo.attribute.VertexBufferObjectAttributes;

/**
 * Compared to a {@link LowMemoryVertexBufferObject}, the
 * {@link HighPerformanceVertexBufferObject} uses <b><u>2x</u> the heap
 * memory</b>, at the benefit of significantly faster data buffering (<b>up to
 * <u>5x</u> faster!</b>).
 * 
 * @see {@link LowMemoryVertexBufferObject} when to prefer a
 *      {@link LowMemoryVertexBufferObject} instead of a
 *      {@link HighPerformanceVertexBufferObject}
 * 
 *      <p>
 *      
 *      </p>
 * 
 * @author  
 * @since 14:42:18 - 15.11.2011
 */
public class HighPerformanceVertexBufferObject extends VertexBufferObject {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	protected final float[] mBufferData;
	protected final FloatBuffer mFloatBuffer;

	// ===========================================================
	// Constructors
	// ===========================================================

	public HighPerformanceVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pAutoDispose, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) {
		super(pVertexBufferObjectManager, pCapacity, pDrawType, pAutoDispose, pVertexBufferObjectAttributes);

		this.mBufferData = new float[pCapacity];
		this.mFloatBuffer = this.mByteBuffer.asFloatBuffer();
	}

	public HighPerformanceVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final float[] pBufferData, final DrawType pDrawType, final boolean pAutoDispose, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) {
		super(pVertexBufferObjectManager, pBufferData.length, pDrawType, pAutoDispose, pVertexBufferObjectAttributes);
		this.mBufferData = pBufferData;

		this.mFloatBuffer = this.mByteBuffer.asFloatBuffer();
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public float[] getBufferData() {
		return this.mBufferData;
	}

	public int getHeapMemoryByteSize() {
		return this.getByteCapacity();
	}

	public int getNativeHeapMemoryByteSize() {
		return this.getByteCapacity();
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	protected void onBufferData() {
		// TODO Check if, and how mow this condition affects performance.
		this.mFloatBuffer.position(0);
		this.mFloatBuffer.put(this.mBufferData);

		Device.getDevice().getGlES20().glBufferData(IGLES20.GL_ARRAY_BUFFER, this.mByteBuffer.capacity(), this.mByteBuffer, this.mUsage);

	}

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
